package com.theydream.qeechain.model.map;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapController;
import com.theydream.qeechain.globaldata.GlobalData;

public class MyLocationManager {
	private static int count=0;
	private static final MyLocationManager _instance = new MyLocationManager();
	private static final long MINIMUM_DISTANCECHANGE_FOR_UPDATE = 10; // in Meters
	private static final long MINIMUM_TIME_BETWEEN_UPDATE = 1000; // in Milliseconds
	private static final long POINT_RADIUS = 20; // in Meters
	private static final long ITEM_ALERT_EXPIRATION = -1;
	private static final String ITEM_ALERT_INTENT ="com.theydream.pocketpets.map.Map";
	private LocationManager locationManager;
	private Location alertlocation = new Location("alertlocation");
	private PendingIntent proximityIntent;
	Context mCtx;
	private MapController mMapController;
	private boolean alwaysLocateMe=false;
	private MyLocationManager() {
	}
	public static synchronized MyLocationManager getMyLocationManager() {
	return _instance;
	}
	// Initialize the reference to the location manager and register a listener
	// to obtain location updates
	public void setLocationManager(Context context, MapController mapController, LocationManager locman) {
	mCtx = context;
	mMapController=mapController;
	locationManager = locman;
	locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,MINIMUM_TIME_BETWEEN_UPDATE, MINIMUM_DISTANCECHANGE_FOR_UPDATE,new MyLocationListener());
	locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MINIMUM_TIME_BETWEEN_UPDATE, MINIMUM_DISTANCECHANGE_FOR_UPDATE,new MyLocationListener());
	}
	// Add a proximity alert for a specific location. In my implementation I
	// allow for only one alert to be active at a time
	// If there is an existing alert, then I cancel it before I set up the new
	// alert
	public void addProximityAlert(String item, double latitude,
	double longitude) {
	// If an alert already exists, then cancel the alert
	if (proximityIntent != null) {
		locationManager.removeProximityAlert(proximityIntent);
		//proximityIntent = null;
	}
	// Create a new Intent for the specific location and then turn it into a
	// pending intent
	Intent intent = new Intent(ITEM_ALERT_INTENT);
	intent.putExtra("item", item);
	// The proximity alert will result in the Intent being broadcast when


	// the intent is fired
	proximityIntent = PendingIntent.getBroadcast(mCtx, 0, intent,0);
	// alert location keeps track of the location for which the current
	// alert is active
	// This is used by the locationlistener to compute the distance to the
	// alert location
	alertlocation.setLatitude(latitude);
	alertlocation.setLongitude(longitude);
	// Register the alert for the location selected by the user
//	locationManager.addProximityAlert(latitude, // the latitude of the
//	// central point of the
//	// alert region
//	longitude, // the longitude of the central point of the alert
//	// region
//	POINT_RADIUS, // the radius of the central point of the alert
//	// region, in meters
//	ITEM_ALERT_EXPIRATION, // time for this proximity alert, in
//	// milliseconds, or -1 to indicate no
//	// expiration
//	proximityIntent // will be used to generate an Intent to fire
//	// when entry to or exit from the alert region
//	// is detected
//	);
	// Register the Intent filter and specify the Broadcast Receiver that
	// will respond when the Pending Intent is fired by the proximity alert
	IntentFilter filter = new IntentFilter(ITEM_ALERT_INTENT);
	mCtx.registerReceiver(new ItemProximityIntentReceiver(), filter);
	}
	public class MyLocationListener implements LocationListener {
	// Whenever the user's location is updated, it computes the distance to
	// the alert location and displays a toast message
	public void onLocationChanged(Location location) {
	int distance = (int)location.distanceTo(alertlocation);
	if (location != null) {
		int lat = (int) (location.getLatitude() * 1E6);
		int lng = (int) (location.getLongitude() * 1E6);
		if (alwaysLocateMe)
		{
			GeoPoint point = new GeoPoint(lat, lng);
			mMapController.animateTo(point);
			mMapController.setZoom(19);
		}
		
	}
//	Toast.makeText(mCtx, "Distance from Pet:" + distance+" Meters",
//	Toast.LENGTH_LONG).show();
	GlobalData.getInstance().setUserLocation(location);
	
	}
	public void onStatusChanged(String s, int i, Bundle b) {
	}
	public void onProviderDisabled(String s) {
	}
	public void onProviderEnabled(String s) {
	}
	}
	
	public void changeLocateMe()
	{
		alwaysLocateMe=!alwaysLocateMe;
	}
	
	}
	
